
Page({
	data: {
		scrollHeight:'',
    	scrollWidth:'',
    	// 搜索框的border样式
    	border: '1px solid skyblue',
    	scrollTop:'',
    	query: {
    		keywords:'',
    		start:'',
    		count:'',
    		page:1
    	},
    	isGetData: false,
    	searchDatas: [],
    	// 加载更多的相关设置
    	loadmore:'加载更多...',
    	show: 'none',
    	noDataShow:'none'
	},
	onLoad: function() {
		var _this = this;
		// 设置导航标题
		wx.setNavigationBarTitle({
		  title: '搜索图书'
		})
		// 得到系统信息
	    wx.getSystemInfo({
	      success: function(res) {
	        console.log(res.windowWidth);
	        console.log(res.windowHeight); 
	        _this.setData({
	          scrollHeight: res.windowHeight - 45,
	          scrollWidth: res.windowWidth
	        })
	      }
	    });
	},
	scrollEvent: function(e) {
    	console.log(e);
 	},
	bindConfirm: function(e) {
		var _this = this;
		var keywords = e.detail.value.trim();
		if(keywords === '') {
			keywords = '金庸';
		}
		console.log(keywords);
		this.setData({
			scrollTop:0,
			query: {
				keywords: keywords,
				start:0,
    			count:10,
    			page:1
			}
		});
		wx.request({
			url: `https://api.douban.com/v2/book/search?q=${_this.data.query.keywords}&start=${_this.data.query.start}&count=${_this.data.query.count}`,
			success: function(res) {
				if(res.data.books.length === 0) {
					_this.setData({
						noDataShow: 'block',
						show:'none'
					});
				} else {
					_this.setData({
						isGetData:true,
						loadmore:'加载更多...'		
					});
					if(res.data.books.length >= _this.data.query.count) {
						_this.setData({
							show:'block',
							noDataShow: 'none'
						});
					} else {
						_this.setData({
							show:'none',
							noDataShow: 'none'
						});
					}
				}
				
				console.log(res);
			    _this.setData({
			    	searchDatas:res.data.books
			    })
			    console.log(_this.data.searchDatas);
			}
		});
	},
	bindFocus: function() {
		this.setData({
			border: '1px solid skyblue'
		})
	},
	bindBlur: function() {
		this.setData({
			border: 'none'
		})
	},
	// 通过event向将要跳转过去的页面传递参数
	 book_detail: function(event) {
	 	var id = event.currentTarget.id;
	  	console.log(id);
	    wx.navigateTo({
	        url: '../detail/detail?id='+id
	    })
	 },
	loadMore: function() {
		var _this = this;
		var datas = Object.assign([], this.data.searchDatas);
		var start = this.data.query.page*this.data.query.count;
		var count = this.data.query.count;
		var page = this.data.query.page + 1;
		var keywords = this.data.query.keywords;
		if(!this.data.isGetData) {
			return;
		}
		wx.request({
			url: `https://api.douban.com/v2/book/search?q=${_this.data.query.keywords}&start=${start}&count=${_this.data.query.count}`,
			success: function(res) {
				if(res.data.books.length === 0) {
					_this.setData({
						loadmore: '没有更多数据了'
					})
					return;
				}
			    _this.setData({
			    	searchDatas: datas.concat(res.data.books),
			    	query: {
			    		keywords:keywords,
			    		start: start,
			    		count: count,
			    		page: page
			    	}
			    });
			    _this.setData({
					isGetData:true
				});
			}
		});
		this.setData({
			isGetData:false
		});
	}

})